package com.lsj.ssmTest.core;

import com.lsj.ssmTest.core.entity.AjaxResult;
import com.lsj.ssmTest.core.exception.LoginException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Lsj
 * @version 1.0
 * @date 2021/6/29 20:51
 * 全局异常处理
 */
@ControllerAdvice
public class ExceptionControllerAdvice {
    @ExceptionHandler(value = {LoginException.class})
    @ResponseBody
    public AjaxResult<String> handlerException(Exception e, HttpServletResponse response) {
        return new AjaxResult<>(403, e.getMessage());
    }

    @ExceptionHandler(value = {Exception.class})
    public String error(Exception e, Model model) {
        model.addAttribute("error", e);
        return "error";
    }
}
